package cn.clementine.core.utils;

public class FixedSizeStringBuilder {
    private final int capacity;
    private final StringBuilder stringBuilder;

    public FixedSizeStringBuilder(int capacity) {
        this.capacity = capacity;
        this.stringBuilder = new StringBuilder(capacity);
    }

    public void append(String str) {
        if (stringBuilder.length() < capacity) {
            int remainingCapacity = capacity - stringBuilder.length();
            int strLength = str.length();
            if (strLength <= remainingCapacity) {
                stringBuilder.append(str);
            } else {
                stringBuilder.append(str, 0, remainingCapacity);
            }
        }
    }

    public String toString() {
        return stringBuilder.toString();
    }
}